package team.seekers.catering.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {
    private static final String dateFormat = "yyyy-MM-dd hh:mm:ss";

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);
    }
    /**
     * 反格式化
     */
    public static Date DeformatDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date parse = null;
        try {
            parse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
    /**
     * 在日期date上增加amount天 。
     *
     * @param date   处理的日期，非null
     * @param amount 要加的天数，可能为负数
     */
    public static Date addDays(Date date, int amount) {
        Calendar now =Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+amount);
        return now.getTime();
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.formatDate(new Date()));
        System.out.println(DateUtil.formatDate(DateUtil.addDays(new Date(), -1)));
    }

    /**
     * 比较是否逾期
     * @param overDate 逾期时间
     * @param concurrentDate 现在的时间
     * @return true表示逾期
     */
    public static boolean compareDate(Date overDate, Date concurrentDate) {
       return (overDate.getTime()-concurrentDate.getTime())>0;
    }
    /**
     * 得到当天的凌晨时间
     */
    public static Long getTodDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(date.getTime());
        long daytime1 = 0;
        try {
            daytime1 = simpleDateFormat.parse(today).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return daytime1;
    }
    /**
     * 得到第二天的凌晨
     */
    public static Long getSecDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }


}
